//
//  LoginViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import "DataBaseHandle.h"
#import "User.h"

@interface LoginViewController ()<LoginViewDelegate>

@property (nonatomic, retain) LoginView *loginV;

@end

@implementation LoginViewController

- (void)dealloc
{
    [_loginV release];
    [_titleName release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"用户登录";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    self.loginV = [[LoginView alloc] initWithFrame:self.view.frame];
    self.loginV.delegate = self;
    [self.view addSubview:self.loginV];
    [_loginV release];
}

- (void)preshButtonlogin
{
    

    
    User *user = [[DataBaseHandle shareDataBase] selectByName:self.loginV.textF1.text];
        if ([user.name isEqualToString:self.loginV.textF1.text] && [user.password isEqualToString:self.loginV.textF2.text]) {
//            NSLog(@"登陆成功");
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            
            NSString *filePath = [path stringByAppendingPathComponent:@"str.xml"];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
            if ([dic objectForKey:self.loginV.textF2.text] == self.loginV.textF1.text) {
                user.name = user.name;
                user.password = user.password;
                user.state = @"1";
            }
            
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                User *tempUser = [[User alloc] init];
                tempUser.name = user.name;
                tempUser.password = user.password;
                tempUser.state = @"1";
                
//                [[DataBaseHandle shareDataBase] createUserTable];
                [[DataBaseHandle shareDataBase] updateDataWithUser:tempUser];
                [[DataBaseHandle shareDataBase] createActivityTable];
                [[DataBaseHandle shareDataBase] createMovieTable];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
                
                
            }];
        }

//            NSLog(@"用户名或密码错误");
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
        }

//    if ([arr containsObject:user]) {
//        NSLog(@"登陆成功");
//    } else {
//        NSLog(@"登陆失败");
//    }


- (void)preshButton
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    [registerVC release];
}

- (void)barAction:(UIBarButtonItem *)sender
{
    if (sender == self.navigationItem.leftBarButtonItem) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
